package com.common.pattern.pipeline;

import com.common.pattern.pipeline.constant.ResultCode;
import com.common.pattern.pipeline.constant.ResultMessage;
import lombok.Data;

import java.io.Serializable;

/**
 * @Author: xuxiang
 * @Date: 2023-8-10
 * @Description:
 */
@Data
public class Result<T> implements Serializable {

    private Result() {
    }

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    public static Result<String> ok() {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMessage.SUCCESS);
        return result;
    }

    public static Result<String> ok(Integer resultCode, String resultMessage) {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        return result;
    }

    public static <T> Result<T> ok(Integer resultCode, String resultMessage, T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMessage.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result<String> fail() {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMessage.ERROR);
        return result;
    }

    public static Result<String> fail(Integer resultCode, String resultMessage) {
        Result<String> result = new Result<>();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        return result;
    }

    public static <T> Result<T> fail(Integer resultCode, String resultMessage, T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMessage.ERROR);
        result.setData(data);
        return result;
    }
}
